package com.yuji.aacar.controller.others;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.Const;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.canceledit.CancelEditText;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <li>Package:com.yuji.aacar.controller.others</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/9</li>
 * <li>Description:  编辑内容 </li>
 */

public class EditContentActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.edit_content_edit)
    CancelEditText editContentEdit;
    @BindView(R.id.edit_content_submit)
    Button editContentSubmit;


    private String fromPage;
    private String content;
    private String type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_edit_content);
        ButterKnife.bind(this);
        initTitle(true, "填写信息", null);
        super.onCreate(savedInstanceState);

        initUI();
    }

    private void initUI() {
        fromPage = getIntent().getStringExtra("from");
        content = getIntent().getStringExtra("content");
        type = getIntent().getStringExtra("type");

        if (fromPage.contentEquals(Const.FROM_WRITE_ORDER)) {
            if (null != type && null != content) {
                if (type.contentEquals("name")) {
                    editContentEdit.setText(content);
                } else if (type.contentEquals("phone")) {
                    editContentEdit.setText(content);
                    editContentEdit.setInputType(InputType.TYPE_CLASS_PHONE);
                } else if (type.contentEquals("carno")) {
                    editContentEdit.setText(content);
                } else if (type.contentEquals("address")) {
                    editContentEdit.setText(content);
                }
            }
        } else {
            if (null != type && null != content) {
                if (type.contentEquals("nickName")) {
                    editContentEdit.setText(content);
                } else if (type.contentEquals("userName")) {
                    editContentEdit.setText(content);
                } else if (type.contentEquals("sex")) {
                    editContentEdit.setText(content);
                }
            }
        }

    }

    @OnClick(R.id.edit_content_submit)
    public void onClick(View view) {
        Tools.closeKeyBroad(EditContentActivity.this, view);
        String str = editContentEdit.getText().toString().trim();
        if (!TextUtils.isEmpty(str)){
            if (type.contentEquals("name")){
                CoreEventCenter.postMessage(EventConst.CHANGE_NAME,str);
                finish();
            }else if (type.contentEquals("phone")){
                if (Tools.isPhoneNumberValid(str)){
                    CoreEventCenter.postMessage(EventConst.CHANGE_PHONE,str);
                    finish();
                } else {
                    showToast("请输入正确的手机号！");
                }
            }else if (type.contentEquals("carno")){
                if (Tools.isCarNo(str)){
                    String head = str.substring(0,1);
                    String body = str.substring(1,str.length()).toUpperCase();
                    CoreEventCenter.postMessage(EventConst.CHANGE_CARNO,head+body);
                    finish();
                } else {
                    showToast("请输入正确的车牌号！");
                }
            }else if (type.contentEquals("address")){
                CoreEventCenter.postMessage(EventConst.CHANGE_ADDRESS_DETAIL,str);
                finish();
            }else if (type.contentEquals("nickName")){
                CoreEventCenter.postMessage(EventConst.PERSONAL_NICKNAME,str);
                finish();
            }else if (type.contentEquals("userName")){
                CoreEventCenter.postMessage(EventConst.PERSONAL_NAME,str);
                finish();
            }else if (type.contentEquals("sex")){
                if (str.contentEquals("男")||str.contentEquals("女")){
                    CoreEventCenter.postMessage(EventConst.PERSONAL_SEX,str);
                    finish();
                } else {
                    showToast("请输入男或者女");
                }
            }
        } else {
            showToast("请输入内容！");
        }

    }
}
